import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
import '../../pages/danger/danger_list2.dart';
import '../../pages/gmap/gmap.dart';
import '../../pages/gmap/gmapHelper.dart';
import '../../pages/gmap/gpsHelper.dart';
import '../../spaces/recordspace/record_list.dart';

import '../../model/inspectreport.dart';
import 'InspectCard.dart';

class InspectReportInfoPage extends StatefulWidget {
  @override
  InspectReportInfoPage(this.X1);
  final InspectReport X1;

  @override
  InspectReportInfoPageState createState() => InspectReportInfoPageState();
}

class InspectReportInfoPageState extends State<InspectReportInfoPage> {
  late InspectReport X1;
  int currentPage = 0;

  @override
  void initState() {
    X1 = widget.X1;
    super.initState();
  }

  List<LayerOptions> _mapLayers() {
    var layers = GMapHelper.NewLayerList();
    var gpsList = GpsHelper.StringToPointList2(X1.GpsRemark);
    if (gpsList.length > 2) {
      layers.add(GMapHelper.PolylineLayer(gpsList, 2.0, Colors.red));
      var f1 =
          GpsHelper.transform(gpsList.first.latitude, gpsList.first.longitude);
      var f2 =
          GpsHelper.transform(gpsList.last.latitude, gpsList.last.longitude);
      //layers.add(GMapHelper.MyMarker2( f1,"起点"));
      //layers.add(GMapHelper.MyMarker2(f2,"终点"));
    }

    return layers;
  }

  LatLng _getLocation() {
    var gpsList = GpsHelper.StringToPointList(X1.GpsRemark);
    if (gpsList.length > 0) return gpsList.first;

    return LatLng(42, 123);
  }

  Widget getBody(int x) {
    var p2 = GpsHelper.transformP(_getLocation());
    if (x == 0) return InspectReportAddBody(context);
    if (x == 1)
      return MyMap(
        location: p2,
        layers: _mapLayers(),
        zoom: 17.0,
      );
    if (x == 2) return RecordListPage(X1.Guid, false);
    if (x == 3) return DangerListPage(false, X1.Guid, false);
    return Container();
    // return EventListPage(X1,false);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("巡查报告"),
      ),
      bottomNavigationBar: new BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          currentIndex: currentPage,
          items: bottomTabs,
          fixedColor: Colors.blue,
          onTap: (index) {
            setState(() {
              currentPage = index;
            });
          }),
      body: getBody(currentPage),
    );
  }

  InspectReportAddBody(BuildContext context) =>
      SingleChildScrollView(child: InspectReportAddFields(context));

  //  定义底部导航列表
  List<BottomNavigationBarItem> bottomTabs = [
    new BottomNavigationBarItem(icon: new Icon(Icons.book), label: ('巡查信息')),
    new BottomNavigationBarItem(icon: new Icon(Icons.map), label: ('巡查路线')),
    new BottomNavigationBarItem(
        icon: new Icon(Icons.camera_alt), label: ('巡查记录')),
    new BottomNavigationBarItem(icon: new Icon(Icons.list), label: ('问题上报')),
  ];

  InspectReportAddFields(BuildContext context) {
    return Container(
        margin: EdgeInsets.only(top: 8.0),
        child: new Column(children: <Widget>[
          InspectCard.BodyHeader(context, X1),
        ]));
  }
}
